from django.shortcuts import render, HttpResponse, redirect, reverse
from app01 import models
from django.http.response import JsonResponse
from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator


# Create your views here.


def user_list(request):
    """
    用户表展示
    :param requset:
    :return:
    """
    user_all = models.User.objects.all()
    return render(request, 'user_list.html', {'user_all': user_all})


class UserForm(forms.Form):
    username = forms.CharField(label='用户名', widget=forms.TextInput, required=True)
    full_name = forms.CharField(label='姓名', widget=forms.TextInput, required=True)
    password = forms.CharField(label='密码', widget=forms.PasswordInput, required=True)
    phone_number = forms.CharField(label='手机号', widget=forms.TextInput, required=False)

    def clean_username(self):
        value = self.cleaned_data.get('username')
        if models.User.objects.filter(username=value):
            raise ValidationError('用户名已存在')
        return value


def user_add(request):
    form_obj = UserForm()
    if request.method == 'POST':
        form_obj = UserForm(request.POST)
        username = request.POST.get('username')
        full_name = request.POST.get('full_name')
        password = request.POST.get('password')
        phone_number = request.POST.get('phone_number')
        if form_obj.is_valid():
            models.User.objects.create(username=username, full_name=full_name,
                                       password=password,
                                       phone_number=phone_number)
            return redirect(reverse('app01:user_list'))

    return render(request, 'user_add.html', {'form_obj': form_obj})


def user_del(request, *args):
    """
    删除
    :param request:
    :param args:
    :return:
    """
    id = int(*args)
    user_del = models.User.objects.filter(pk=id)
    if not user_del:
        return HttpResponse('数据不存在')
    else:
        user_del.delete()
    return redirect(reverse('app01:user_list'))
    user_all = models.User.objects.all()
    return render(request, 'user_list.html', {'user_all': user_all})


def user_edit(request, *args):
    """
    修改
    :param request:
    :return:
    """
    error = ''
    pk = int(*args)
    use = models.User.objects.filter(pk=pk)
    if not use:
        return HttpResponse('数据不存在')
    use_obj = use[0]
    if request.method == 'POST':
        use_username = request.POST.get('use_username')
        use_full_name = request.POST.get('use_full_name')
        use_phone_number = request.POST.get('use_phone_number')
        if  not use_username:
            error = '不能为空'
        elif use_obj.username == use_username and use_obj.full_name == use_full_name:
            error = '数据未修改'
        else:
            print(use_full_name)
            use_obj.username = use_username
            use_obj.full_name = use_full_name
            use_obj.phone_number = use_phone_number
            use_obj.save()
            return redirect(reverse('app01:user_list'))
    return render(request, 'user_edit.html', {'use_obj': use_obj, 'error': error})


def Property_list(request):
    """
    资产表展示
    :param requset:
    :return:
    """
    Property_all = models.Property.objects.all()
    return render(request, 'Property_list.html', {'Property_all': Property_all})


class PropertyForm(forms.Form):
    material_name = forms.CharField(label='物资名称', widget=forms.TextInput, required=True)
    material_description = forms.CharField(label='物资描述', widget=forms.TextInput, required=True)
    material_number = forms.CharField(label='物资编号', widget=forms.TextInput, required=True)
    buy_time = forms.CharField(label='购买时间', widget=forms.TextInput)
    purchaser = forms.CharField(label='购买人', widget=forms.TextInput, required=True)
    Experiment_id = forms.ChoiceField(label='所属实验室', choices=models.Experiment.objects.values_list('pk', 'name'),
                                      required=True)

    def clean_supplies_name(self):
        value = self.cleaned_data.get('material_name')
        if models.Property.objects.filter(material_name=value):
            raise ValidationError('物资名已存在')
        return value


def Property_add(request):
    """
    资产表添加
    :param request:
    :return:
    """
    print(999)
    form_obj = PropertyForm()
    if request.method == 'POST':
        print(555)
        form_obj = PropertyForm(request.POST)
        if form_obj.is_valid():
            Pro_material_name = request.POST.get('material_name')
            material_description = request.POST.get('material_description')
            Pro_material_number = request.POST.get('material_number')
            Pro_buy_time = request.POST.get('buy_time')
            purchaser = request.POST.get('purchaser')
            Experiment_id = request.POST.get('Experiment_id')
            print(22)

            models.Property.objects.create(material_name=Pro_material_name, material_description=material_description,
                                               material_number=Pro_material_number, buy_time=Pro_buy_time,
                                           purchaser=purchaser,
                                           Experiment_id=Experiment_id)
            return redirect(reverse('app01:Property_list'))
    return render(request, 'Property_add.html', {'form_obj': form_obj})


def Property_del(request, *args):
    """
    资产表删除
    :param request:
    :return:
    """
    id = int(*args)
    Pro_del = models.Property.objects.filter(pk=id).delete()
    if Pro_del:
        return HttpResponse('数据不存在')
    else:
        user_del.delete()
    return redirect(reverse('app01:Property_list'))
    Pro_all = models.Property.objects.all()
    return render(request, 'Property_list.html', {'Pro_all': Pro_all})


def Property_edit(request, *args):
    """
    资产表修改
    :param request:
    :param args:
    :return:
    """
    id = int(*args)
    Pro_obj = models.Property.objects.filter(pk=id).first()
    if not Pro_obj:
        return HttpResponse('数据不存在')
    if request.method == 'POST':
        Pro_material_name = request.POST.get('Pro_material_name')
        Pro_material_number = request.POST.get('Pro_material_number')
        Pro_buy_time = request.POST.get('Pro_buy_time')
        Pro_Experiment_name = request.POST.get('Pro_Experiment_name')
        Pro_obj.material_name = Pro_material_name
        Pro_obj.material_number = Pro_material_number
        Pro_obj.buy_time = Pro_buy_time
        Pro_obj.Experiment_name = Pro_Experiment_name
        Pro_obj.save()
        return redirect('app01:Property_list')
    Pro_all = models.Property.objects.all()
    return render(request, 'Property_edit.html', {'Pro_all': Pro_all,"Pro_obj":Pro_obj})


def Experiment_list(request):
    """
    实验室表展示
    :param request:
    :return:
    """
    Experiment_all = models.Experiment.objects.all()
    return render(request, 'Experiment_list.html', {'Experiment_all': Experiment_all})


class ExperimentForm(forms.Form):
    name = forms.CharField(label='实验室名称', widget=forms.TextInput, required=True)
    floor = forms.CharField(label='楼层', widget=forms.TextInput, required=True)
    room_number = forms.CharField(label='房间号', widget=forms.TextInput, required=True)
    principal = forms.CharField(label='负责人', widget=forms.TextInput, required=True)

    def clean_Experiment_name(self):
        value = self.cleaned_data.get('name')
        if models.Experiment.objects.filter(name=value):
            raise ValidationError('实验室名已存在')
        return value


def Experiment_add(request):
    """
    实验室表添加
    :param request:
    :return:
    """
    form_obj = ExperimentForm()
    if request.method == 'POST':
        form_obj = ExperimentForm(request.POST)
        Exp_name = request.POST.get('name')
        Exp_floor = request.POST.get('floor')
        Exp_room_number = request.POST.get('room_number')
        Exp_principal = request.POST.get('principal')
        User_id = request.POST.getlist('User_id')
        Exp_obj = models.Experiment.objects.create(name=Exp_name, floor=Exp_floor,
                                                   room_number=Exp_room_number, principal=Exp_principal)
        if form_obj.is_valid():
            Exp_obj.User.set(User_id)
            return redirect(reverse('app01:Experiment_list'))
    return render(request, 'Experiment_add.html', {'form_obj': form_obj})


def Experiment_del(request, *args):
    """
    实验室表删除
    :param request:
    :return:
    """
    pk = int(*args)
    models.Experiment.objects.filter(pk=pk).delete()

    return redirect('app01:Experiment_list')


def Experiment_edit(request, *args):
    """
    实验室表修改
    :param request:
    :return:
    """
    id = int(*args)
    Exp_obj = models.Experiment.objects.filter(pk=id).first()
    print(111)
    if not Exp_obj:
        return HttpResponse('数据不存在')
    if request.method == 'POST':
        Exp_name = request.POST.get('Exp_name')
        Exp_floor = request.POST.get('Exp_floor')
        Exp_room_number = request.POST.get('Exp_room_number')
        Exp_principal = request.POST.get('Exp_principal')
        # user_id = request.POST.getlist('user_id')
        print(222)
        Exp_obj.name = Exp_name
        Exp_obj.floor = Exp_floor
        Exp_obj.room_number = Exp_room_number
        Exp_obj.principal = Exp_principal
        Exp_obj.save()
        # Exp_obj.users.set(user_id)
        return redirect(reverse('app01:Experiment_list'))
    Pro_all = models.Property.objects.all()
    return render(request, 'Experiment_edit.html', {'Pro_all': Pro_all})
